package com.outsourcing.bottle.ui;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.ext.SatelliteMenu;
import android.view.ext.SatelliteMenu.SateliteClickedListener;
import android.view.ext.SatelliteMenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.aviary.android.feather.Constants;
import com.aviary.android.feather.FeatherActivity;
import com.google.gson.Gson;
import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.db.LoginUserInfoTable;
import com.outsourcing.bottle.db.PushNoticeInfoTable;
import com.outsourcing.bottle.domain.ExFeedInfoEntry;
import com.outsourcing.bottle.domain.ExchangePicFeedInfo;
import com.outsourcing.bottle.domain.ExchangePicInfoEntry;
import com.outsourcing.bottle.domain.LoginUserInfoEntry;
import com.outsourcing.bottle.domain.PushNoticeInfo;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.ImageLoader;
import com.outsourcing.bottle.util.ImageThumbnail;
import com.outsourcing.bottle.util.ServiceUtils;
import com.outsourcing.bottle.util.TextUtil;
import com.outsourcing.bottle.util.UICore;
import com.outsourcing.bottle.util.Utility;
import com.outsourcing.bottle.widget.CustomPopupWindow;
import com.outsourcing.bottle.widget.TryRefreshableView;

public class ExchangePicFeedDetailActivity extends BasicActivity implements
		BasicUIEvent, Callback, OnClickListener {

	private final int menu_uploadphoto = 105;
	private final int menu_exprofile = 104;
	private final int menu_exphoto = 103;
	private final int menu_gainbt = 102;
	private final int menu_sendbt = 101;
	private final int user_baseinfo_notfull = 100;
	private final int complete_info = 1;
	
	private static final int EXEU_EXCHANGE_PIC_INFO = 0;
	private static final int EXEU_EXCHANGE_PIC_INFO_FAILED = 1;
	private static final int EXEU_REFRESH_PIC_INFO = 2;
	private static final int EXEU_LIKE_OPERATION = 3;
	private static final int EXEU_UNLIKE_OPERATION = 4;
	private static final int EXEU_LIKE_OP_PICEX_SUCCESS = 5;
	private static final int EXEU_LIKE_OP_PICEX_FAILED = 6;
	protected static final int REQUESTCODE_FOR_DRAW = 7;
	protected static final int EXEU_ALLOW_PAINT_SUCCESS = 8;
	private static final int EXEU_DELETE_LOCATION = 9;
	private static final int EXEU_DELETE_LOCATION_SUCCESS = 10;
	private static final int EXEU_DELETE_LOCATION_FAILED = 11;
	private static final int EXEU_DELETE_LOCATION_ROOT = 12;
	private static final int download_image = 13;
	private static final String TAG = ExchangePicFeedDetailActivity.class.getSimpleName();

	Handler handler;
	ImageView topbar_back;
	ImageView topbar_menu;
	float longitude, latidute;
	String location;
	Bitmap bitmap = null;
	private int ouid;
	private int page = 1;
	private int picid;
	private ImageLoader imageLoader;
	private ExchangePicFeedInfo mExPicInfo;
	private TryRefreshableView rv;
	private LinearLayout ll_content;
	private ScrollView sv;
	
//	private ImageView messageView;
	private LinearLayout bottomLayout;
	private ImageView avatarView;
	private TextView contentView;
	private TextView btnCount;
	private int newNoticeCount;
	private int newMessageCount;
	private int newBottleFeedCount;
	private int newExchangeFeedCount;
	private int newMaybeKownCount;
	private String message_content;
	private String message_avatar;
	private ImageLoader userImageLoader;
	private PushBroadcastReceiver receiver;
	private PushNoticeInfo pushInfo;

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.exchange_pic_root);
		handler = new Handler(this);

		ouid = getIntent().getIntExtra("ouid", 0);
		picid = getIntent().getIntExtra("picid", 0);

		topbar_back = (ImageView) findViewById(R.id.topbar_back);
		topbar_menu = (ImageView) findViewById(R.id.topbar_menu);
		topbar_menu.setVisibility(View.VISIBLE);
		topbar_menu.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ExchangePicFeedDetailActivity.this, HomeActivity.class);
				intent.putExtra("currentItem", 0);
				startActivity(intent);
				finish();
			}
		});
		topbar_back.setOnClickListener(this);
//		messageView = (ImageView) findViewById(R.id.notice_message_count);
//		messageView.setOnClickListener(this);
		bottomLayout = (LinearLayout) findViewById(R.id.notice_bottom_layout);
		bottomLayout.setOnClickListener(this);
		avatarView = (ImageView) findViewById(R.id.notice_avatar);
		contentView = (TextView) findViewById(R.id.notice_text);
		btnCount = (TextView) findViewById(R.id.notice_number);
//		messageView.setVisibility(View.VISIBLE);
		// refresh_view = (ElasticScrollView) findViewById(R.id.refresh_view);
		// refresh_view.setonRefreshListener(new
		// ElasticScrollView.OnRefreshListener() {
		//
		//
		// @Override
		// public void onRefresh() {
		// page =1;
		// UICore.eventTask(ExchangePicFeedDetailActivity.this,
		// ExchangePicFeedDetailActivity.this, EXEU_REFRESH_PIC_INFO, null,
		// null);
		// }
		// });
		imageLoader = new ImageLoader(this, AppContext.BottleTimelineIcon);
		userImageLoader = new ImageLoader(this, AppContext.UserAvatarIcon);

		sv = (ScrollView) findViewById(R.id.trymySv);
		rv = (TryRefreshableView) findViewById(R.id.trymyRV);
		rv.mfooterView = (View) findViewById(R.id.tryrefresh_footer);
		rv.sv = sv;
		ll_content = (LinearLayout) findViewById(R.id.ll_content);
		// 隐藏mfooterView
		rv.mfooterViewText = (TextView) findViewById(R.id.tryrefresh_footer_text);
		rv.mfooterViewText.setVisibility(View.INVISIBLE);

		// 监听是否加载刷新
		rv.setRefreshListener(new TryRefreshableView.RefreshListener() {

			@Override
			public void onRefresh() {
				if (rv.mRefreshState == 4) {
					page = 1;
					UICore.eventTask(ExchangePicFeedDetailActivity.this,
							ExchangePicFeedDetailActivity.this,
							EXEU_REFRESH_PIC_INFO, null, null);
				} else if (rv.mfooterRefreshState == 4) {
					page = 1;
					UICore.eventTask(ExchangePicFeedDetailActivity.this,
							ExchangePicFeedDetailActivity.this,
							EXEU_REFRESH_PIC_INFO, null, null);
				}

			}
		});
		initLikePopupWindow();
		initSateMenu();
		UICore.eventTask(this, this, EXEU_EXCHANGE_PIC_INFO, "", null);
	}

	private void initSateMenu() {
		SatelliteMenu menu = (SatelliteMenu) findViewById(R.id.menu);
		menu.setVisibility(View.VISIBLE);
		if (CanvasWidth >= 480) {
			menu.setSatelliteDistance(200);
		} else {
			menu.setSatelliteDistance(100);
		}
		List<SatelliteMenuItem> items = new ArrayList<SatelliteMenuItem>();
		items.add(new SatelliteMenuItem(menu_exprofile, R.drawable.menu_exprofile));
		items.add(new SatelliteMenuItem(menu_exphoto, R.drawable.menu_exphoto));
		items.add(new SatelliteMenuItem(menu_gainbt, R.drawable.menu_gainbt));
		items.add(new SatelliteMenuItem(menu_sendbt, R.drawable.menu_sendbt));
		items.add(new SatelliteMenuItem(menu_uploadphoto, R.drawable.menu_uploadphoto));
		menu.addItems(items);
		menu.setOnItemClickedListener(new SateliteClickedListener() {

			public void eventOccured(int id) {
				switch (id) {
				case menu_sendbt: // 扔瓶子
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent throwIntent = new Intent(
										ExchangePicFeedDetailActivity.this,
										ChooseBottleActivity.class);
								throwIntent.putExtra("type", "throw");
								startActivity(throwIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_gainbt: // 捞瓶子
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent tryIntent = new Intent(
										ExchangePicFeedDetailActivity.this,
										ChooseBottleActivity.class);
								tryIntent.putExtra("type", "try");
								startActivity(tryIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_exphoto: // 交换照片
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent exchangeInfoIntent = new Intent(
										ExchangePicFeedDetailActivity.this,
										ChooseFriendActivity.class);
								exchangeInfoIntent
										.putExtra(
												"choose_friend_type",
												AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE);
								startActivity(exchangeInfoIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_exprofile: // 交换资料
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								LoginUserInfoTable userTable = new LoginUserInfoTable();
								LoginUserInfoEntry userEntry = userTable
										.getLoginUserInfo(AppContext
												.getInstance().getLogin_uid());
								if (userEntry.getBasicinfo_fullfill() == 0) {
									handler.sendEmptyMessage(user_baseinfo_notfull);
								} else {
									Intent exchangeFileIntent = new Intent(
											ExchangePicFeedDetailActivity.this,
											ChooseFriendActivity.class);
									exchangeFileIntent
											.putExtra(
													"choose_friend_type",
													AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE);
									startActivity(exchangeFileIntent);
								}
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_uploadphoto:
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent intent = new Intent(ExchangePicFeedDetailActivity.this, ExpandEditActivity.class);
								Bundle extras = new Bundle();
								extras.putString("from_type", AppContext.UPLOAD_PHOTO);
								extras.putInt("albumid", 0);
								extras.putInt("ouid", AppContext.getInstance().getLogin_uid());
								intent.putExtras(extras);
								startActivity(intent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				default:
					break;
				}
			}
		});
	}

	
	
	@Override
	protected void onResume() {
		super.onResume();
		if(AppContext.getInstance().isFromExchangePicDetail()){
			AppContext.getInstance().setFromExchangePicDetail(false);
			page = 1;
			UICore.eventTask(this, this, EXEU_REFRESH_PIC_INFO, "", null);
		}
		
	}
	
	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case EXEU_EXCHANGE_PIC_INFO:
			getExchangePicFeed(ouid, page, picid);
			break;
		case EXEU_REFRESH_PIC_INFO:
			refreshExchangePicFeed(ouid, page, picid);
			break;
		case EXEU_LIKE_OPERATION:
			likeIOperationPicex(ouid, picid, 0);
			break;
		case EXEU_UNLIKE_OPERATION:
			likeIOperationPicex(ouid, picid, 1);
			break;	
		case EXEU_ALLOW_PAINT_SUCCESS:
			int optype = (Integer)obj;
			setExpicPaint(ouid,picid,optype);
			break;
		case EXEU_DELETE_LOCATION:
			ExFeedInfoEntry mExFeedPicEntry = (ExFeedInfoEntry)obj;
			deleteLocation(String.valueOf(mExFeedPicEntry.getLocation_objtype_2()),String.valueOf(mExFeedPicEntry.getLocation_objid_2()));
			break;
		case EXEU_DELETE_LOCATION_ROOT:
			ExchangePicInfoEntry mExEntry = (ExchangePicInfoEntry)obj;
			deleteLocation(String.valueOf(mExEntry.getLocation_objtype()),String.valueOf(mExEntry.getLocation_objid()));
			break;
		default:
			break;
		}
	}

	private void setExpicPaint(int ouid, int picid,int optype) {
		StringBuffer url = new StringBuffer(UrlConfig.set_expic_paint);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		url.append("&picid=" + picid);
		url.append("&optype=" + optype);
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				JSONObject object = new JSONObject(result);
				JSONObject resultObj = object.getJSONObject("results");
				int authok = resultObj.getInt("success");
				if (authok == 0) {
					String errmsg = resultObj.getString("errmsg");
					Message msg = Message.obtain(handler, EXEU_EXCHANGE_PIC_INFO_FAILED, errmsg);
					handler.sendMessage(msg);
				} else {
					String successmsg = resultObj.getString("successmsg");
					Message msg = Message.obtain(handler, EXEU_ALLOW_PAINT_SUCCESS, successmsg);
					handler.sendMessage(msg);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void likeIOperationPicex(int ouid, int picid, int liketype) {
		StringBuffer url = new StringBuffer(UrlConfig.likeop_picex);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		url.append("&picid=" + picid);
		url.append("&liketype=" + liketype);

		ServiceUtils.dout("likeIOperationPicex url: " + url.toString());

		try {
			String results = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("likeIOperationPicex result:" + results);
			if (null != results) {
				JSONObject obj = new JSONObject(results);
				JSONObject resultObj = obj.getJSONObject("results");
				int success = resultObj.getInt("success");
				String errmsg = resultObj.getString("errmsg");
				String successmsg = resultObj.getString("successmsg");
				if (success == 1) {
					Message message = handler.obtainMessage(
							EXEU_LIKE_OP_PICEX_SUCCESS, successmsg);
					handler.sendMessage(message);
				} else {
					Message message = handler.obtainMessage(
							EXEU_LIKE_OP_PICEX_FAILED, errmsg);
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	View view = null;
	private View mLikeView;
	private CustomPopupWindow mLikePopview;
	private ImageView bigImageView ;
	ExchangePicInfoEntry exPicOuidEntry;
	private Bitmap imageBitmap;
	
	
	private void setViewValue() {
		view = LayoutInflater.from(this).inflate(
				R.layout.exchange_pic_feed_detail, null);
		RemoteImageView iv_ovatar_2 = (RemoteImageView) view
				.findViewById(R.id.iv_ex_pic_ovatar_2);
		TextView tv_ex_feed = (TextView) view.findViewById(R.id.tv_ex_feed);
		TextView tv_feed_time = (TextView) view.findViewById(R.id.tv_feed_time);
		TextView tv_comment = (TextView) view
				.findViewById(R.id.comments_ellipsis_text);
		RemoteImageView iv_ovatar_right = (RemoteImageView) view
				.findViewById(R.id.iv_ex_pic_ovatar_3);
		RelativeLayout content_photo = (RelativeLayout) view
				.findViewById(R.id.rl_bottle_content_photo);
		bigImageView = (ImageView) view.findViewById(R.id.photo);
		Button bt_ex_comment = (Button) view.findViewById(R.id.bt_pic_comment);
		Button bt_ex_forward = (Button) view.findViewById(R.id.bt_pic_forward);

		LinearLayout feed_comment = (LinearLayout) view
				.findViewById(R.id.ll_feed_comment_content);

		exPicOuidEntry = mExPicInfo.getPic_info();

		iv_ovatar_2.setDefaultImage(R.drawable.avatar_default_small);
		iv_ovatar_2.setImageUrl(exPicOuidEntry.getSavatar());
		
		iv_ovatar_right.setDefaultImage(R.drawable.avatar_default_small);
		iv_ovatar_right.setImageUrl(exPicOuidEntry.getRavatar());
		
		iv_ovatar_2.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent spaceIntent = new Intent(ExchangePicFeedDetailActivity.this, UserSpaceActivity.class);
				spaceIntent.putExtra("ouid", exPicOuidEntry.getSuid());
				startActivity(spaceIntent);
			}
		});
		iv_ovatar_right.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent spaceIntent = new Intent(ExchangePicFeedDetailActivity.this, UserSpaceActivity.class);
				spaceIntent.putExtra("ouid", exPicOuidEntry.getRuid());
				startActivity(spaceIntent);
			}
		});

		String nickName = null;
		if (ouid!= AppContext.getInstance()
				.getLogin_uid()) {
			nickName ="<font color='#DA4A37'>"
				+ exPicOuidEntry.getSnickname() + "</font>";
		} else {
			nickName = "<font color='#3F99D8'>"
					+ exPicOuidEntry.getSnickname() + "</font>";
		}
		
		tv_ex_feed.setText(Html.fromHtml(nickName
				+ exPicOuidEntry.getExfeed_send_content()));

		if ((null != exPicOuidEntry.getExfeed_send_time() && exPicOuidEntry
				.getExfeed_send_time().length() > 0)
				|| (null != exPicOuidEntry.getExfeed_send_location() && exPicOuidEntry
						.getExfeed_send_location().length() > 0)) {
			tv_feed_time.setVisibility(View.VISIBLE);
			String time = (null != exPicOuidEntry.getExfeed_send_time() && exPicOuidEntry
					.getExfeed_send_time().length() > 0) ? (" #" + exPicOuidEntry.getExfeed_send_time()): "";
			String location = (null != exPicOuidEntry.getExfeed_send_location() && exPicOuidEntry
					.getExfeed_send_location().length() > 0) ? (" @"+exPicOuidEntry
					.getExfeed_send_location()) : "";
			tv_feed_time.setText(time + location);
			if (!TextUtils.isEmpty(location)) {
				tv_feed_time.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						
							if(exPicOuidEntry.getLocation_delete_enable() == 1){
								final Context dialogContext = new ContextThemeWrapper(
										ExchangePicFeedDetailActivity.this,
										android.R.style.Theme_Light);
								String[] choices = new String[2];
								choices[0] = getString(R.string.bottle_txt_check_location);
								choices[1] = getString(R.string.bottle_txt_clean_location);

								final ListAdapter adapter = new ArrayAdapter<String>(
										dialogContext,
										android.R.layout.simple_list_item_1,
										choices);

								final AlertDialog.Builder builder = new AlertDialog.Builder(
										dialogContext);
								builder.setTitle(R.string.system_info);
								builder.setSingleChoiceItems(
										adapter,
										-1,
										new DialogInterface.OnClickListener() {

											public void onClick(
													DialogInterface dialog,
													int which) {
												dialog.dismiss();
												if (which == 0) {
													dialog.dismiss();
													Intent intent = new Intent(
															ExchangePicFeedDetailActivity.this,
															MapViewActivity.class);
													intent.putExtra("longitude",
															exPicOuidEntry.getExfeed_send_lng());
													intent.putExtra("latidute",
															exPicOuidEntry.getExfeed_send_lat());
													intent.putExtra("location",
															exPicOuidEntry.getExfeed_send_location());
													startActivity(intent);
												} else {
													UICore.eventTask(ExchangePicFeedDetailActivity.this, ExchangePicFeedDetailActivity.this, EXEU_DELETE_LOCATION_ROOT, "", exPicOuidEntry);
												}

											}
										});
								builder.create().show();
						} else {	
							
						final Context dialogContext = new ContextThemeWrapper(
								ExchangePicFeedDetailActivity.this,
								android.R.style.Theme_Light);
						String[] choices = new String[1];
						choices[0] = getString(R.string.bottle_txt_check_location);

						final ListAdapter adapter = new ArrayAdapter<String>(
								dialogContext,
								android.R.layout.simple_list_item_1,
								choices);

						final AlertDialog.Builder builder = new AlertDialog.Builder(
								dialogContext);
						builder.setTitle(R.string.system_info);
						builder.setSingleChoiceItems(adapter, -1,
								new DialogInterface.OnClickListener() {
									public void onClick(
											DialogInterface dialog,
											int which) {
										dialog.dismiss();
										Intent intent = new Intent(
												ExchangePicFeedDetailActivity.this,
												MapViewActivity.class);
										intent.putExtra("longitude",
												exPicOuidEntry.getExfeed_send_lng());
										intent.putExtra("latidute",
												exPicOuidEntry.getExfeed_send_lat());
										intent.putExtra("location",
												exPicOuidEntry.getExfeed_send_location());
										startActivity(intent);
									}
								});
						builder.create().show();}
						
					}
				});
			}
		
		} else {
			tv_feed_time.setVisibility(View.GONE);
		}

		if (null != exPicOuidEntry.getExfeed_send_comment()
				&& exPicOuidEntry.getExfeed_send_comment().length() > 0) {
			String commentTips = "<font color='#000000'>"
					+ TextUtil.R("exchange_feed_comment_title") + "</font>";
			tv_comment.setText(Html.fromHtml(commentTips
					+ TextUtil.htmlEncode(exPicOuidEntry.getExfeed_send_comment())));
		} else {
		}
		LinearLayout comment_button_like = (LinearLayout) view
		.findViewById(R.id.comment_button_like);
		if (!TextUtils.isEmpty(exPicOuidEntry.getExfeed_pic())) {
			content_photo.setVisibility(View.VISIBLE);

			Button bt_drawing = (Button)view.findViewById(R.id.bt_drawing);
			Button bt_allow_draw = (Button)view.findViewById(R.id.bt_allow_draw);
			ServiceUtils.dout(TAG+" Exfeed_pic_big:"+exPicOuidEntry.getExfeed_pic_big());
//			imageLoader.DisplayImage(exPicOuidEntry.getExfeed_pic_big(), photo,
//					R.drawable.photo_wrapper);
			new Thread() {
				public void run() {
					downloadImage();
				};
			}.start();
			bigImageView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					if (!TextUtils.isEmpty(exPicOuidEntry.getExfeed_pic_tuya())) {
						showCommentPicFeedDialog(exPicOuidEntry);
					}else {
						byte[] tempBytes = null;
						try {
							String filename = ServiceUtils.convertUrlToFileName(exPicOuidEntry.getExfeed_pic());
							tempBytes = ServiceUtils.readStream(new FileInputStream(new File(
									android.os.Environment.getExternalStorageDirectory() + "/" + AppContext.BottleTimelineIcon + "/" + filename)));
						} catch (Exception e) {
							e.printStackTrace();
						}
						Intent bigPicIntent = new Intent(ExchangePicFeedDetailActivity.this, PhotoDetailActivity.class);
						bigPicIntent.putExtra("pic_big", exPicOuidEntry.getExfeed_pic_big());
						bigPicIntent.putExtra("thumbnail", tempBytes);
						ExchangePicFeedDetailActivity.this.startActivity(bigPicIntent);
					}
					
				}
			});
			
			if (exPicOuidEntry.getExfeed_pic_allowtuya() == 1) {
				(view.findViewById(R.id.ll_layout_drawing)).setVisibility(View.VISIBLE);
				bt_drawing.setVisibility(View.VISIBLE);
				bt_drawing.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						ByteArrayOutputStream baos = new ByteArrayOutputStream();
						imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
						byte[] tempBytes = baos.toByteArray();
						
						File file = Utility.getNextFileName();
						FileOutputStream fs = null;
						try {
							fs = new FileOutputStream(file);
							fs.write(tempBytes);
						} catch (IOException e) {
							e.printStackTrace();
						} finally {
							try {
								if (null != fs) {
									fs.close();	
								}
							} catch (IOException e) {
								e.printStackTrace();
							}	
						}
						
						File resultfile = Utility.getNextFileName();
						Uri fileUri = Uri.fromFile(resultfile);
						Intent newIntent = new Intent( ExchangePicFeedDetailActivity.this, FeatherActivity.class );
						newIntent.putExtra( "From_Type", Constants.ACTIVITY_DRAW );
						newIntent.setData( fileUri );
						newIntent.putExtra( "API_KEY", Utility.API_KEY );
						newIntent.putExtra( "output", Uri.parse( "file://" +  resultfile.getAbsolutePath() ) );
						newIntent.putExtra( Constants.EXTRA_OUTPUT_FORMAT, Bitmap.CompressFormat.JPEG.name() );
						newIntent.putExtra( Constants.EXTRA_OUTPUT_QUALITY, 100 );
						newIntent.putExtra( Constants.EXTRA_TOOLS_DISABLE_VIBRATION, true );
						final DisplayMetrics metrics = new DisplayMetrics();
						getWindowManager().getDefaultDisplay().getMetrics( metrics );
						int max_size = Math.min( metrics.widthPixels, metrics.heightPixels );
						max_size = (int) ( (double) max_size / 0.8 );
						newIntent.putExtra( "max-image-size", max_size );
						newIntent.putExtra( "effect-enable-borders", true );
//						mSessionId = StringUtils.getSha256( System.currentTimeMillis() + Utility.API_KEY );
//						newIntent.putExtra( "output-hires-session-id", mSessionId );
//						startActivity(newIntent);
						startActivityForResult(newIntent, REQUESTCODE_FOR_DRAW);
						overridePendingTransition(R.anim.translate_right_in, R.anim.translate_left_in);
						
					}
				});
			}else {
				bt_drawing.setVisibility(View.GONE);
			}
			
			if (exPicOuidEntry.getExfeed_pic_enabletuya()== 0) {
				bt_allow_draw.setVisibility(View.GONE);
			}else {
				(view.findViewById(R.id.ll_layout_drawing)).setVisibility(View.VISIBLE);
				bt_allow_draw.setVisibility(View.VISIBLE);
				if (exPicOuidEntry.getExfeed_pic_enabletuya()== 1) {
					bt_allow_draw.setText(TextUtil.R("bottle_txt_allow_draw"));
					bt_allow_draw.setBackgroundResource(R.drawable.btn_red_try);
					bt_allow_draw.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
						
							//TODO  允许涂鸦
							UICore.eventTask(ExchangePicFeedDetailActivity.this, ExchangePicFeedDetailActivity.this, EXEU_ALLOW_PAINT_SUCCESS, "", 1);
						}
					});
				}else {
					bt_allow_draw.setText(TextUtil.R("bottle_txt_not_allow_draw"));
					bt_allow_draw.setBackgroundResource(R.drawable.btn_gray_try);
					bt_allow_draw.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							UICore.eventTask(ExchangePicFeedDetailActivity.this, ExchangePicFeedDetailActivity.this, EXEU_ALLOW_PAINT_SUCCESS, "", 0);
						}
					});
				}
			}
			
		} else {
			content_photo.setVisibility(View.GONE);
		}
		
		if (exPicOuidEntry.getExfeed_like_enable() == 1) {
			comment_button_like.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.photo_pill));
			comment_button_like
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							
							showInfoPopupWindow(v,picid,ouid);
						}
					});
		} else {
			comment_button_like
					.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.photo_pill_dark));
		}
		ImageView mLikeIcon = (ImageView) view
				.findViewById(R.id.comment_button_like_icon);

		switch (exPicOuidEntry.getExfeed_pic_liked()) {
		case 0:
			mLikeIcon
					.setImageResource(R.drawable.like_gray2);
			break;
		case 1:
			mLikeIcon
					.setImageResource(R.drawable.like_red2);
			break;
		case 2:
			mLikeIcon
					.setImageResource(R.drawable.like_black2);
			break;
		default:
			break;
		}
		
		
		bt_ex_comment.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent commentIntent = new Intent(
						ExchangePicFeedDetailActivity.this,
						ExpandEditActivity.class);
				Bundle commentBundle = new Bundle();
				commentBundle.putString("from_type", AppContext.REPLYPIC_PICEX);
				commentBundle.putInt("ouid", ouid);
				commentBundle.putInt("picid", picid);
				commentIntent.putExtras(commentBundle);
				startActivityForResult(commentIntent, 0);

			}
		});
		if (exPicOuidEntry.getExfeed_forward_enable() == 1) {
			bt_ex_forward.setVisibility(View.VISIBLE);
			bt_ex_forward.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					Intent friendIntent = new Intent(
							ExchangePicFeedDetailActivity.this,
							ChooseFriendActivity.class);
					friendIntent.putExtra("choose_friend_type",
							AppContext.CHOOSE_FRIEND_FORWARD_PHOTO_TYPE);
					friendIntent.putExtra("picid", picid);
					friendIntent.putExtra("pic_uid", ouid);
					startActivity(friendIntent);
					AppContext.getInstance().setFromExchangePicDetail(true);
				}
			});
		} else {
			bt_ex_forward.setVisibility(View.GONE);
		}

		/******************************* 留言分割线 ***********************************/

		List<ExFeedInfoEntry> mExFeedPicEntries = mExPicInfo.getExfeeds_list();
		if (null != mExFeedPicEntries && mExFeedPicEntries.size() > 0) {
			for (int j = 0; j < mExFeedPicEntries.size(); j++) {
				final ExFeedInfoEntry mExFeedPicEntry = mExFeedPicEntries
						.get(j);
				View feed_child = LayoutInflater.from(this).inflate(
						R.layout.exchange_pic_feed_item, null);
				RemoteImageView mFeedCommentAvatar = (RemoteImageView) feed_child
						.findViewById(R.id.comment_profile_photo);

				ImageView link = (ImageView) feed_child
						.findViewById(R.id.iv_feed_comment_white);
				TextView tv_nickName = (TextView) feed_child
						.findViewById(R.id.tv_feed_comment_nickname);
				TextView tv_location = (TextView) feed_child
						.findViewById(R.id.tv_feed_comment_time);
				try {
					mFeedCommentAvatar.setDefaultImage(R.drawable.avatar_default_small);
					mFeedCommentAvatar.setImageUrl(mExFeedPicEntry.getExfeed_avatar());
				} catch (Exception e) {
					e.printStackTrace();
					mFeedCommentAvatar
							.setImageResource(R.drawable.avatar_default_small);
				}
				mFeedCommentAvatar
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								Intent spaceIntent = new Intent(ExchangePicFeedDetailActivity.this, UserSpaceActivity.class);
								spaceIntent.putExtra("ouid", mExFeedPicEntry.getExfeed_uid());
								startActivity(spaceIntent);
							}
						});
				String feed_name = null;
				String comment = null;
				if (mExFeedPicEntry.getExfeed_uid()!= AppContext.getInstance()
						.getLogin_uid()) {
					feed_name = (!TextUtils.isEmpty(mExFeedPicEntry.getExfeed_nickname())) ? ("<font color='#DA4A37'>"
							+ mExFeedPicEntry.getExfeed_nickname()+ "</font>")
							: "";
				} else {
					feed_name = (!TextUtils.isEmpty(mExFeedPicEntry.getExfeed_nickname())) ? ("<font color='#3F99D8'>"
							+ mExFeedPicEntry.getExfeed_nickname() + "</font>")
							: "";
				}
				// 动态（0）、评论（1）、是喜欢动态（2）、是不喜欢动态（3）
				switch (mExFeedPicEntry.getExfeed_type()) {
				case 0:
					link.setImageResource(R.drawable.feed_gray);
					
					comment = mExFeedPicEntry.getExfeed_isnew() == 1 ? ("<font color='#DA4A37'>"
							+  TextUtil.htmlEncode(mExFeedPicEntry.getExfeed_content()) + "</font>")
							:  TextUtil.htmlEncode(mExFeedPicEntry.getExfeed_content());
							
					tv_nickName.setText(Html.fromHtml(feed_name
							+comment));
					break;
				case 1:
					link.setVisibility(View.GONE);
					comment = mExFeedPicEntry.getExfeed_isnew() == 1 ? ("<font color='#DA4A37'>"
							+  TextUtil.htmlEncode(mExFeedPicEntry.getExfeed_comment()) + "</font>")
							:  TextUtil.htmlEncode(mExFeedPicEntry.getExfeed_comment());
					
					tv_nickName.setText(Html.fromHtml(feed_name
							+ comment));
					break;
				case 2:
					link.setImageResource(R.drawable.like_red3);
					comment = mExFeedPicEntry.getExfeed_isnew() == 1 ? ("<font color='#DA4A37'>"
							+  TextUtil.htmlEncode(mExFeedPicEntry.getExfeed_content()) + "</font>")
							:  TextUtil.htmlEncode(mExFeedPicEntry.getExfeed_content());
					
					tv_nickName.setText(Html.fromHtml(feed_name
							+ comment));
					break;
				case 3:
					link.setImageResource(R.drawable.like_black3);
					comment = mExFeedPicEntry.getExfeed_isnew() == 1 ? ("<font color='#DA4A37'>"
							+  TextUtil.htmlEncode(mExFeedPicEntry.getExfeed_content()) + "</font>")
							:  TextUtil.htmlEncode(mExFeedPicEntry.getExfeed_content());
					
					tv_nickName.setText(Html.fromHtml(feed_name
							+ comment));
					break;
				default:
					break;
				}

				String timeComent = (mExFeedPicEntry.getExfeed_time() != null && mExFeedPicEntry
						.getExfeed_time().length() > 0) ? ("<font color='#666666'>"
						+ "#" + mExFeedPicEntry.getExfeed_time() + "</font>")
						: "";
				final String location = (mExFeedPicEntry.getExfeed_location() != null && mExFeedPicEntry
						.getExfeed_location().length() > 0) ? ("<font color='#666666'>"
						+ "@" + mExFeedPicEntry.getExfeed_location() + "</font>")
						: "";
				tv_location.setText(Html.fromHtml(timeComent + location));
				if (!TextUtils.isEmpty(location)) {
					tv_location.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {

							if(location.length()>0){
								if(mExFeedPicEntry.getLocation_delete_enable_2() == 1){
									final Context dialogContext = new ContextThemeWrapper(
											ExchangePicFeedDetailActivity.this,
											android.R.style.Theme_Light);
									String[] choices = new String[2];
									choices[0] = getString(R.string.bottle_txt_check_location);
									choices[1] = getString(R.string.bottle_txt_clean_location);

									final ListAdapter adapter = new ArrayAdapter<String>(
											dialogContext,
											android.R.layout.simple_list_item_1,
											choices);

									final AlertDialog.Builder builder = new AlertDialog.Builder(
											dialogContext);
									builder.setTitle(R.string.system_info);
									builder.setSingleChoiceItems(
											adapter,
											-1,
											new DialogInterface.OnClickListener() {

												public void onClick(
														DialogInterface dialog,
														int which) {
													dialog.dismiss();
													if (which == 0) {
														dialog.dismiss();
														Intent intent = new Intent(
																ExchangePicFeedDetailActivity.this,
																MapViewActivity.class);
														intent.putExtra("longitude",
																mExFeedPicEntry
																		.getExfeed_lng());
														intent.putExtra("latidute",
																mExFeedPicEntry
																		.getExfeed_lat());
														intent.putExtra(
																"location",
																mExFeedPicEntry
																		.getExfeed_location());
														startActivity(intent);
													} else {
														UICore.eventTask(ExchangePicFeedDetailActivity.this, ExchangePicFeedDetailActivity.this, EXEU_DELETE_LOCATION, "", mExFeedPicEntry);
													}

												}
											});
									builder.create().show();
							} else {
								final Context dialogContext = new ContextThemeWrapper(
										ExchangePicFeedDetailActivity.this,
										android.R.style.Theme_Light);
								String[] choices = new String[1];
								choices[0] = getString(R.string.bottle_txt_check_location);

								final ListAdapter adapter = new ArrayAdapter<String>(
										dialogContext,
										android.R.layout.simple_list_item_1,
										choices);

								final AlertDialog.Builder builder = new AlertDialog.Builder(
										dialogContext);
								builder.setTitle(R.string.system_info);
								builder.setSingleChoiceItems(adapter, -1,
										new DialogInterface.OnClickListener() {
											public void onClick(
													DialogInterface dialog,
													int which) {
												dialog.dismiss();
												Intent intent = new Intent(
														ExchangePicFeedDetailActivity.this,
														MapViewActivity.class);
												intent.putExtra("longitude",
														mExFeedPicEntry
																.getExfeed_lng());
												intent.putExtra("latidute",
														mExFeedPicEntry
																.getExfeed_lat());
												intent.putExtra(
														"location",
														mExFeedPicEntry
																.getExfeed_location());
												startActivity(intent);
											}
										});
								builder.create().show();
								}
							}
							
						}
					});
				}
				feed_comment.addView(feed_child);
			}
		}
		ll_content.addView(view);
		rv.mfooterViewText.setVisibility(View.VISIBLE);
	}

	
	public void showInfoPopupWindow(View achorView, int picid, int ouid) {
		this.ouid = ouid;
		this.picid = picid;
		if (mLikePopview.isShowing()) {
			mLikePopview.dismiss();
		} else {
			// mLikePopview.showAtLocation(achorView, Gravity.RIGHT, 0, 0);
			mLikePopview.showAsDropDown(achorView, 60, -60);
		}
	}

	/** 喜欢pop */
	private void initLikePopupWindow() {

		LayoutInflater mLayoutInflater = LayoutInflater.from(this);
		mLikeView = mLayoutInflater.inflate(R.layout.pop_bottle_like_left, null);
		ImageView iv_unlike = (ImageView) mLikeView
				.findViewById(R.id.iv_unlike);
		ImageView iv_like = (ImageView) mLikeView.findViewById(R.id.iv_like);
		iv_unlike.setOnClickListener(this);
		iv_like.setOnClickListener(this);
		mLikePopview = new CustomPopupWindow(mLikeView,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		mLikePopview.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.like_frame_left));
		mLikePopview.setOutsideTouchable(true);
		mLikePopview.update();
		mLikePopview.setTouchable(true);
		mLikePopview.setFocusable(true);
	}
	
	private void getExchangePicFeed(int ouid, int page, int picid) {
		StringBuffer url = new StringBuffer(UrlConfig.get_picex_picfeeds);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		url.append("&picid=" + picid);
		url.append("&count=20");
		url.append("&page=" + page);
		ServiceUtils.dout("getExchangePicFeed url: " + url.toString());
		try {
			String results = HttpUtils.doGet(url.toString());

			if (results == null) {
				return;
			}
			ServiceUtils.dout("getExchangePicFeed results:" + results);
			Gson gson = new Gson();
			mExPicInfo = gson.fromJson(results, ExchangePicFeedInfo.class);
			if (mExPicInfo.getResults().getAuthok() == 1) {
				PushNoticeInfoTable table = new PushNoticeInfoTable();
				pushInfo = table.getPushNoticeInfo(AppContext.getInstance().getLogin_uid());
				if (null!=pushInfo) {
					newBottleFeedCount = pushInfo.getNew_btfeed_count();
					newExchangeFeedCount = pushInfo.getNew_exfeed_count();
					newMessageCount = pushInfo.getNew_message_count();
					newNoticeCount = pushInfo.getNew_notice_count();
					message_content = pushInfo.getMessage_content();
					message_avatar = pushInfo.getMessage_avatar();
					newMaybeKownCount = pushInfo.getNew_maybeknow_count();
				}
				Message message = handler.obtainMessage(EXEU_EXCHANGE_PIC_INFO,
						mExPicInfo.getResults().getErrmsg());
				handler.sendMessage(message);

			} else {
				Message message = handler.obtainMessage(
						EXEU_EXCHANGE_PIC_INFO_FAILED, mExPicInfo.getResults()
								.getErrmsg());
				handler.sendMessage(message);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void refreshExchangePicFeed(int ouid, int page, int picid) {
		StringBuffer url = new StringBuffer(UrlConfig.get_picex_picfeeds);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&ouid=" + ouid);
		url.append("&picid=" + picid);
		url.append("&count=20");
		url.append("&page=" + page);
		ServiceUtils.dout("getExchangePicFeed url: " + url.toString());
		try {
			String results = HttpUtils.doGet(url.toString());

			if (results == null) {
				return;
			}
			ServiceUtils.dout("getExchangePicFeed results:" + results);
			Gson gson = new Gson();
			mExPicInfo = gson.fromJson(results, ExchangePicFeedInfo.class);
			if (mExPicInfo.getResults().getAuthok() == 1) {
				Message message = handler.obtainMessage(EXEU_REFRESH_PIC_INFO,
						mExPicInfo.getResults().getErrmsg());
				handler.sendMessage(message);

			} else {
				Message message = handler.obtainMessage(
						EXEU_EXCHANGE_PIC_INFO_FAILED, mExPicInfo.getResults()
								.getErrmsg());
				handler.sendMessage(message);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case EXEU_EXCHANGE_PIC_INFO:
			setViewValue();
			pushNoticeUpdateUI();
			receiver = new PushBroadcastReceiver();
			IntentFilter filter = new IntentFilter();
			filter.addAction("push");
			registerReceiver(receiver, filter);
			break;
		case EXEU_REFRESH_PIC_INFO:
			// refresh_view.removeChild(view);
			ll_content.removeAllViews();
			setViewValue();
			// refresh_view.onRefreshComplete();
			rv.finishRefresh();
			break;
		case EXEU_EXCHANGE_PIC_INFO_FAILED:
			onToast((String) msg.obj);
			break;
		case EXEU_LIKE_OP_PICEX_SUCCESS:
			onToast((String) msg.obj);
			UICore.eventTask(this, this, EXEU_REFRESH_PIC_INFO, "", null);
			break;
		case EXEU_LIKE_OP_PICEX_FAILED:
			onToast((String) msg.obj);
			UICore.eventTask(this, this, EXEU_REFRESH_PIC_INFO, "", null);
			break;
		case user_baseinfo_notfull:
			LoginUserInfoTable userTable = new LoginUserInfoTable();
			LoginUserInfoEntry userEntry = userTable.getLoginUserInfo(AppContext.getInstance().getLogin_uid());
			showChoseMes(userEntry.getBasicinfo_tip(), complete_info);
			break;
		case EXEU_ALLOW_PAINT_SUCCESS:
			UICore.eventTask(this, this, EXEU_REFRESH_PIC_INFO, "", null);
			break;
		case EXEU_DELETE_LOCATION_SUCCESS:
			if (!TextUtils.isEmpty((String) msg.obj)) {
				onToast((String) msg.obj);
			}
			UICore.eventTask(this, this, EXEU_REFRESH_PIC_INFO, "", null);
			break;
		case EXEU_DELETE_LOCATION_FAILED:
			onToast((String) msg.obj);
			break;
		case download_image:
			Bitmap tempBP =  (Bitmap) msg.obj;
			view.findViewById(R.id.rl_bottle_content_photo).setVisibility(View.VISIBLE);
			view.findViewById(R.id.pull_to_refresh_progress).setVisibility(View.GONE);
			bigImageView.setVisibility(View.VISIBLE);
			if (imageBitmap != null) {
				bigImageView.setImageBitmap(tempBP);
			}
			break;
			
		default:
			break;
		}
		return false;
	}
	
	private void downloadImage() {
		Bitmap tempBitmap = null;
		if (imageBitmap == null) {
			imageBitmap = HttpUtils.getBitmapFromUrl(ExchangePicFeedDetailActivity.this, exPicOuidEntry.getExfeed_pic_big());	
			
		}
		if (null != imageBitmap && imageBitmap.getWidth() > CanvasWidth) {
			float scale = ImageThumbnail.reckonThumbnail(
					imageBitmap.getWidth(), imageBitmap.getHeight(), CanvasWidth - 20, 0);
			tempBitmap = ImageThumbnail.PicZoom(imageBitmap,
					(int) (imageBitmap.getWidth() / scale), (int) (imageBitmap.getHeight() / scale));
		} else {
			tempBitmap = imageBitmap;
		}
		Message message = handler.obtainMessage(download_image, tempBitmap);
		handler.sendMessage(message);
	}
	
	@Override
	public void sysMesPositiveButtonEvent(int what) {
		if (what == complete_info) {
			Intent intent = new Intent(this, SettingActivity.class);
			startActivity(intent);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.topbar_back:
			finish();
			break;
		case R.id.iv_unlike:// 不喜欢
			mLikePopview.dismiss();
			UICore.eventTask(this, this, EXEU_UNLIKE_OPERATION, "", null);
			break;
		case R.id.iv_like:// 喜欢
			mLikePopview.dismiss();
			UICore.eventTask(this, this, EXEU_LIKE_OPERATION, "", null);
			break;
//		case R.id.notice_message_count:		
//			doMessageAction();
//			break;	
		case R.id.notice_bottom_layout:
			doFeedsAction();
			break;
		default:
			break;
		}
	}
	

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// refresh_view.removeChild(view);
		if (null != data) {
			if (data.getBooleanExtra("need_refresh", false)) {
				page = 1;
				UICore.eventTask(this, this, EXEU_REFRESH_PIC_INFO, "", null);
			}
		}
		if (resultCode == RESULT_OK) {
			if(REQUESTCODE_FOR_DRAW == requestCode){
				
				Intent commentIntent = new Intent(this,
						ExpandEditActivity.class);
				Bundle commentBundle = new Bundle();
				commentBundle.putString("from_type",
						AppContext.PAINT_PICEX);
				commentBundle.putInt("ouid", ouid);
				commentBundle.putInt("picid", picid);
				commentBundle.putString("return_type", ExchangePicFeedDetailActivity.class.getSimpleName());
				commentIntent.putExtras(commentBundle);
				startActivity(commentIntent);
			}			
		}
		
	}
	
	public void doMessageAction() {
		
		final Context dialogContext = new ContextThemeWrapper(this,
				android.R.style.Theme_Light);
		String[] choices = new String[2];
		if (newNoticeCount > 0) {
			choices[0] = newNoticeCount + getString(R.string.bottle_txt_notice_list);
		} else {
			choices[0] = getString(R.string.bottle_txt_notice_list);
		}
		if (newMessageCount > 0) {
			choices[1] = newMessageCount + getString(R.string.bottle_txt_message_list);
		} else {
			choices[1] = getString(R.string.bottle_txt_message_list);
		}
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (which == 0) {
							Intent intent = new Intent(ExchangePicFeedDetailActivity.this, NoticeListActivity.class);
							startActivity(intent);
						} else {
							Intent intent = new Intent(ExchangePicFeedDetailActivity.this, MessageListActivity.class);
							startActivity(intent);
						}
					}
				});
		builder.create().show();
	}
	
	public void doFeedsAction() {
		final Context dialogContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);
		final List<String> choices = new ArrayList<String>(); 
		if (newBottleFeedCount > 0) {
			choices.add(newBottleFeedCount + getString(R.string.bottle_txt_new_bottle_feed));
		}
		if (newExchangeFeedCount > 0) {
			choices.add(newExchangeFeedCount + getString(R.string.bottle_txt_new_exchange_feed));
		}
		if (newNoticeCount > 0) {
			choices.add(newNoticeCount + getString(R.string.bottle_txt_notice_list));
		}
		if (newMessageCount > 0) {
			choices.add(newMessageCount + getString(R.string.bottle_txt_message_list));
		}
		if (newMaybeKownCount > 0) {
			choices.add(newMaybeKownCount + getString(R.string.bottle_txt_new_maybe_kown));
		}
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext, android.R.layout.simple_list_item_1, choices);
		final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_bottle_feed)) != -1) {
							Intent intent = new Intent(ExchangePicFeedDetailActivity.this, HomeActivity.class);
							AppContext.getInstance().setCurrentItem(1);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_exchange_feed)) != -1) {
							Intent intent = new Intent(ExchangePicFeedDetailActivity.this, HomeActivity.class);
							AppContext.getInstance().setCurrentItem(0);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_notice_list)) != -1) {
							Intent intent = new Intent(ExchangePicFeedDetailActivity.this, NoticeListActivity.class);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_message_list)) != -1) {
							Intent intent = new Intent(ExchangePicFeedDetailActivity.this, MessageListActivity.class);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_maybe_kown)) != -1) {
							Intent intent = new Intent(ExchangePicFeedDetailActivity.this, HomeActivity.class);
							AppContext.getInstance().setCurrentItem(2);
							AppContext.getInstance().setGotoMaybeKown(true);
							startActivity(intent);
						}
					}
				});
		builder.create().show();
	}
	
	public void pushNoticeUpdateUI() {
		if (newBottleFeedCount + newExchangeFeedCount + newMessageCount + newNoticeCount + newMaybeKownCount > 0) {
			bottomLayout.setVisibility(View.VISIBLE);
			contentView.setText(message_content);
			btnCount.setText(newBottleFeedCount + newExchangeFeedCount + newMessageCount + newNoticeCount + newMaybeKownCount  + "");
			userImageLoader.DisplayImage(message_avatar, avatarView, R.drawable.avatar_default_big);
		} else {
			bottomLayout.setVisibility(View.GONE);
		}
	}
	
	public class PushBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				newBottleFeedCount = bundle.getInt("bottle_feed_count");
				newExchangeFeedCount = bundle.getInt("exchange_feed_count");
				newMessageCount = bundle.getInt("message_count");
				newNoticeCount = bundle.getInt("notice_count");
				message_content = bundle.getString("message_content");
				message_avatar = bundle.getString("message_avatar");
				newMaybeKownCount = bundle.getInt("maybe_kown_count");
				pushNoticeUpdateUI();
			}
		}
	}


	private void showCommentPicFeedDialog(final ExchangePicInfoEntry message) {
		String[] choices = new String[]{TextUtil.R("bottle_txt_feed_comment_pic"),TextUtil.R("bottle_txt_feed_comment_tuya")};
		final Context dialogContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (which == 0) {

							byte[] tempBytes = null;
							try {
								String filename = ServiceUtils.convertUrlToFileName(message.getExfeed_pic());
								tempBytes = ServiceUtils.readStream(new FileInputStream(new File(
										android.os.Environment.getExternalStorageDirectory() + "/" + AppContext.BottleTimelineIcon + "/" + filename)));
							} catch (Exception e) {
								e.printStackTrace();
							}
							Intent bigPicIntent = new Intent(ExchangePicFeedDetailActivity.this, PhotoDetailActivity.class);
							bigPicIntent.putExtra("pic_big", message.getExfeed_pic_big());
							bigPicIntent.putExtra("thumbnail", tempBytes);
							ExchangePicFeedDetailActivity.this.startActivity(bigPicIntent);
							
						} else {
							Intent bigPicIntent = new Intent(ExchangePicFeedDetailActivity.this, PhotoDetailActivity.class);
							bigPicIntent.putExtra("pic_big", message.getExfeed_pic_tuya());
							startActivity(bigPicIntent);
						}
					}
				});
		builder.create().show();
	}
	

	/**
	 * 
	 * @param objtype 要清除的位置的类型：选择项：（1）瓶子动态的位置信息、（2）交换动态留言的位置信息、（3）照片评论的位置信息、（4）私信的位置信息、（5）照片的位置信息
	 * @param objid objid
	 */
	private void deleteLocation(String objtype,String objid) {
		StringBuffer url = new StringBuffer(UrlConfig.delete_location);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&objtype=" + objtype);
		url.append("&objid=" + objid);
		ServiceUtils.dout("deleteLocation url:" + url.toString());
		try {
			String result = HttpUtils.doGet(url.toString());
			ServiceUtils.dout("deleteLocation result:" + result);
			JSONObject obj = new JSONObject(result);
			JSONObject resultObj = obj.getJSONObject("results");
			int success = resultObj.getInt("success");
			String errmsg = resultObj.getString("errmsg");
			String successmsg = resultObj.getString("successmsg");
			if (success == 1) {
				Message message = handler.obtainMessage(
						EXEU_DELETE_LOCATION_SUCCESS, successmsg);
				handler.sendMessage(message);
			} else {
				Message message = handler.obtainMessage(
						EXEU_DELETE_LOCATION_FAILED, errmsg);
				handler.sendMessage(message);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
